﻿import { Dialog } from "primeng/components/dialog/dialog";
import { Component, Injector, ViewChild, OnInit } from "@angular/core";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
    RoleListDto,
    RoleServiceProxy,
    ContactDto,
    ContactServiceProxy,
    TemplateServiceProxy,
    WorkStationDto,
    WorkStationUpdateOrOutput,
    LineTemplateUpdate,
    ProcessDto,
    NodeForTemplateDto,
} from "@shared/service-proxies/service-proxies";
import { Table } from "primeng/components/table/table";
import * as _ from "lodash";
import { finalize } from "rxjs/operators";

@Component({
    selector: "app-contact",
    templateUrl: "./template.component.html",
    styleUrls: ["./template.component.less"],
    animations: [appModuleAnimation()],
})
export class TemplateComponent extends AppComponentBase implements OnInit {
    @ViewChild("dt") dataTable: Table;

    displayDialog: boolean;

    template: LineTemplateUpdate;
    selectedStation: LineTemplateUpdate;
    newTemplate: boolean;
    templates: LineTemplateUpdate[];

    constructor(
        injector: Injector,
        private _templateServiceProxy: TemplateServiceProxy
    ) {
        super(injector);
    }

    cols: any[];
    ngOnInit(): void {
        this.getTemplateList();
    }

    getTemplateList() {
        this._templateServiceProxy.getAllLineTemplate().subscribe((result) => {
            this.templates = result;
        });

        this.cols = [
            { field: "id", header: "编号" },
            { field: "templateName", header: "模板名" },
        ];
    }

    showDialogToAdd() {
        this.newTemplate = true;
        this.template = new LineTemplateUpdate();
        this.displayDialog = true;
    }

    save() {
        this._templateServiceProxy
            .createOrUpdateLineTemplateNodeList(this.template)
            .subscribe((result) => {
                this.getTemplateList();
            });

        this.displayDialog = false;
    }

    delete() {
        let index = this.templates.indexOf(this.template);
        this._templateServiceProxy
            .deleteLineTemplate(this.template.id)
            .subscribe((result) => {
                this.templates = this.templates.filter((val, i) => i != index);
                this.getTemplateList();
            });

        this.template = null;
        this.displayDialog = false;
    }

    onDelete(rowData: NodeForTemplateDto): void {
        this.template.templateNodeList = this.template.templateNodeList.filter(
            (c) => c != rowData
        );
    }

    initProcess() {
        this.template.templateNodeList = [];

        this._templateServiceProxy.getAllStations().subscribe((result) => {
            let i = 1;
            result.forEach((element) => {
                let node = new NodeForTemplateDto();
                node.nodeName = element.stationName;
                node.workStationId = element.id;
                node.workStationName = element.stationName;
                node.orderIndex = i;
                this.template.templateNodeList.push(node);
                i++;
            });
        });
    }

    onRowSelect(event) {
        this.newTemplate = false;
        this.template = this.cloneStation(event.data);
        this.displayDialog = true;
    }

    cloneStation(c: LineTemplateUpdate): LineTemplateUpdate {
        let station = new LineTemplateUpdate();
        for (let prop in c) {
            station[prop] = c[prop];
        }
        return station;
    }
}
